/**
 * <pre>
 *     author : XML
 *     e-mail : 946891360@qq.com
 *     time   : 2019/06/10
 *     version:
 *     desc   :
 * </pre>
 */
import {
    createStackNavigator,
    createSwitchNavigator,
    createAppContainer
} from 'react-navigation'
import WelcomePage from '../page/WelcomePage'
import HomePage from "../page/HomePage";
import DetailPage from "../page/DetailPage";
import FetchDemoPage from "../page/FetchDemoPage";
import AsyncStorageDemoPage from "../page/AsyncStorageDemoPage";
import DataStoreDemoPage from "../page/DataStoreDemoPage";
import {connect} from 'react-redux';
import {createReactNavigationReduxMiddleware, createReduxContainer} from 'react-navigation-redux-helpers';

export const rootCom = 'Init'; //设置跟路由

const InitNavigator = createStackNavigator({
    Welcome: {
        screen: WelcomePage,
        navigationOptions: {
            header: null,
        }
    }
});
const MainNavigator = createStackNavigator({
    HomePage: {
        screen: HomePage,
        navigationOptions: {
            header: null,
        }
    },
    DetailPage: {
        screen: DetailPage,
        navigationOptions: {
            header: null,
        }
    },
    FetchDemoPage: {
        screen: FetchDemoPage,
        navigationOptions: {}
    },
    AsyncStorageDemoPage: {
        screen: AsyncStorageDemoPage,
        navigationOptions: {}
    },
    DataStoreDemoPage: {
        screen: DataStoreDemoPage,
        navigationOptions: {}
    }

});
const switchNavigator = createSwitchNavigator({
    Init: InitNavigator,
    Main: MainNavigator,
}, {
    navigationOptions: {
        header: null,
    }
});
export const RootContainer = createAppContainer(switchNavigator);
/**
 * 1.初始化react-navigation与redux的中间件
 * 该方法的一个很大的作用就是为createReduxContainer的key设置actionSubscribers(行为订阅者),
 * 设置订阅者@https://github.com/react-navigation/redux-helpers/blob/master/src/middleware.js#L23
 * 检测订阅者是否存在@https://github.com/react-navigation/redux-helpers/blob/master/src/middleware.js#L23
 * @type {Middleware}
 */
export const middleware = createReactNavigationReduxMiddleware(
    state => state.nav,
    'root'
);
/**
 * 2.将根导航器组件传递给createReduxContainer 函数
 * 并返回一个将navigation state 和dispatch 函数作为 props的新组件
 * 注意: 要在createReactNavigationReduxMiddleware之后执行
 */
const AppWithNavigationState = createReduxContainer(RootContainer, 'root');
/**
 * State 到Props的映射关系
 * @param state
 * @returns state
 */
const mapStateToProps = state => ({
    state: state.nav,//V2
});
/**
 * 3.连接 React 组件与 Redux store
 */
export default connect(mapStateToProps)(AppWithNavigationState)

